package fr.upvm.lem3.vm3d.ui.swing.dataload;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.nio.file.Paths;
import java.util.*;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;

/**
 * Inherited FileFilter class to facilitate reuse when
 * multiple file filter selections are required. 
 */
public class ExtendedFileFilter extends FileFilter {

    private List<String> extensions;
    private String description;

    public ExtendedFileFilter(String[] exts, String desc) {
        if (exts != null) {
            setExtensions(new ArrayList<String>());

            for (String ext : exts) {

                // Clean array of extensions to remove "."
                // and transform to lowercase.
                getExtensions().add(
                    ext.replace(".", "").trim().toLowerCase()
                );
            }
        } // No else need; null extensions handled below.

        // Using inline if syntax, use input from desc or use
        // a default value.
        // Wrap with an if statement to default as well as
        // avoid NullPointerException when using trim().
        description = (desc != null) ? desc.trim() : "File Filter";
    }

    /**
     * Handles which files are allowed by filter.
     */
    @Override
    public boolean accept(File f) {
    
        // Allow directories to be seen.
        if (f.isDirectory()) 
        	return true;

        // exit if no extensions exist.
        if (getExtensions() == null) 
        	return false;
		
        // Allows files with extensions specified to be seen.
        for (String ext : getExtensions()) {
            if (f.getName().toLowerCase().endsWith("." + ext))
                return true;
        }

        // Otherwise file is not shown.
        return false;
    }

    // 'Files of Type' description
    @Override
    public String getDescription() {
        return description;
    }

	/**
	 * @return the extensions
	 */
	public List<String> getExtensions() {
		return extensions;
	}

	/**
	 * @param extensions the extensions to set
	 */
	public void setExtensions(List<String> extensions) {
		this.extensions = extensions;
	}
	                                    
}
